﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NLog;

namespace CommonMvc.Controllers
{
    public class CommonController : Controller
    {
        IHostingEnvironment _environment;
        ILogger _logger = LogManager.GetLogger("file");
        public CommonController(IHostingEnvironment env) {
            _environment = env;
        }
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Post()
         {
            _logger.Info("请求到方法");
            _logger.Info("ubuntu 图片上传正常测试");
            HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
            try
            {
                var files = Request.Form.Files;
                long size = files.Sum(f => f.Length);

                //size > 100MB refuse upload !
                if (size > 104857600)
                {
                    return new JsonResult(new { Code = 1, Message = "文件过大" });
                }

                List<string> filePathResultList = new List<string>();

                foreach (var file in files)
                {
                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');

                    string filePath = _environment.WebRootPath +  $@"/Files/Files/";

                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }

                    fileName = Guid.NewGuid() + "." + fileName.Split('.')[1];

                    string fileFullName = filePath + fileName;

                    using (FileStream fs = System.IO.File.Create(fileFullName))
                    {
                        file.CopyTo(fs);
                        fs.Flush();
                    }
                    filePathResultList.Add($"/src/Files/{fileName}");
                }
                string message = $"{files.Count} file(s) /{size} bytes 上传成功!";
                return new JsonResult(new { Code = 0, Message = message, src = filePathResultList });
            }
            catch (Exception e) {
                return new JsonResult(new { Code = 1, Message = "上传出错:" + e.ToString() });
            }

        }
            }
            
    #region 
    //   foreach (var file in files)
    //             {
    //                 var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');

    //                 string filePath = _environment.WebRootPath +  $@"/Files/Files/";
           
    //                 if (!Directory.Exists(filePath))
    //                 {
    //                     Directory.CreateDirectory(filePath);
    //                 }

    //                 fileName = Guid.NewGuid() + "." + fileName.Split('.')[1];

    //                 string fileFullName = filePath + fileName;
 
    //                 FileStream fs=new FileStream(fileFullName,FileMode.Create);
    //                 Stream sm=file.OpenReadStream();
    //                 byte[] buffer =new byte[sm.Length];
    //                 sm.Read(buffer,0,buffer.Length);
    //                 fs.Write(buffer,0,buffer.Length);
    //                 fs.Dispose();          
    //                 filePathResultList.Add($"/src/Files/{fileName}");
    //             }
    #endregion
 
    
}